const app = getApp()
Page({
  data: {
    tabArr: {
      curHdIndex: 1,
    },
    loadingHidden: false,//代表出现加载
    hasUserInfo: false,//代表有头像
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    schooldata: {},//全校排名数据
    studentdata: {},//个人学生数据,只有家长角色登录才有.user_type=3
    classdata:{},
    headmaster: 'hide',//班主任
    parent: 'hide',//家长
    isbind: false,//是否绑定
    isTwelve:false,//是否是12中
    usertype:'',
  },
//点击个人学校切换
  handleTab: function (e) {
    var dataId = e.currentTarget.id;
    var obj = {};
    obj.curHdIndex = dataId;
    this.setData({
      tabArr: obj
    })
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: "积分"
    })
    var that = this;
    if (app.globalData.schlink == "" || app.globalData.schlink==null ){
      that.getdomain();
    }
    if (app.globalData.schlink =="wz12.wzzd.cn"){
      that.getschooldata();//获取全校排名数据只有12中有
      that.setData({
        isTwelve:true
      })
    }
  
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

  //通过账号类型判断获取班级数据还是个人数据
    if (getApp().globalData.bindInfo) {
      if (getApp().globalData.bindInfo.user_type == 1) {
        that.setData({
          headmaster: 'show',
          parent: 'hide',
          isbind: app.globalData.isbind,
        });
        that.getclassdata();//班级,班主任就出这个数据
      }
      if (getApp().globalData.bindInfo.user_type == 3) {//家长
        that.setData({
          headmaster: 'hide',
          parent: 'show',
          isbind: app.globalData.isbind,
        });
        that.getstudentdata();//学生个人数据
      }
      that.setData({
        usertype: getApp().globalData.bindInfo.user_type
      });
    }
 
    if (!app.isbindBack) {
      app.isbindBack = function (result) {
        that.setData({
          loadingHidden: true,
        });
        if (result.code == 1) {
          wx.redirectTo({
            url: '../index/index'
          })
          getApp().globalData.bindInfo = result.data;
          if (getApp().globalData.bindInfo.user_type == 1) {
            that.setData({
              headmaster: 'show',
              parent: 'hide'
            });
            that.getclassdata();//班主任出班级数据
          }
          if (getApp().globalData.bindInfo.user_type == 3) {
            that.getstudentdata();//个人数据
            that.setData({
              headmaster: 'hide',
              parent: 'show',
            });
          }
          getApp().globalData.isbind = true;
          that.setData({
            isbind: app.globalData.isbind,
            usertype: getApp().globalData.bindInfo.user_type
          });
        } else if (result.code == 0) {
          getApp().globalData.isbind = false;
          that.setData({
            isbind: false,
          });
          wx.redirectTo({
            url: '../binding/binding'
          })
        }
      }
    }
    that.setData({
      loadingHidden: true,
    });
   
  },

  onReady: function () {

  },

  onShow: function () {
  },

 
  onHide: function () {

  },


  onUnload: function () {

  },

 
  onPullDownRefresh: function () {

  },

  onReachBottom: function () {

  },

  onShareAppMessage: function () {
  },

  //重新获取学校域名
  getdomain: function () {
    var openid = app.globalData.openid;
    wx.request({
      url: getApp().globalData.baseUrl + 'getdomain',
      method: "GET",
      data: {
        openid: openid,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data){
          app.globalData.schlink = res.data;
        }
      }
    })
  },

  //个人排名数据
  getstudentdata: function () {
    if (app.globalData.bindInfo) {
      var uid = app.globalData.bindInfo.user_id;
      var sign = app.globalData.bindInfo.sign;
    }
    if (uid == "" || uid == null || uid == undefined) {
      wx.redirectTo({
        url: '../binding/binding'
      })
      return;
    }
    if (sign == "" || sign == null || sign == undefined) {
      wx.redirectTo({
        url: '../binding/binding'
      })
      return;
    }
    var link = app.globalData.schlink;//学校域名
    var openid = app.globalData.openid;
    var that = this;
    wx.request({
      url: getApp().globalData.baseUrl + 'rank',
      method: "GET",
      data: {
        uid: uid,
        sign:sign,
        openid: openid,
        link:link
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code == 200) {
          var result = res.data.data;
          that.setData({studentdata: result });
        }
        that.setData({
          loadingHidden: true,
        });
      }
    })
  },

  //班级排名数据
  getclassdata: function () {
    if (app.globalData.bindInfo) {
      var uid = app.globalData.bindInfo.user_id;
      var sign = app.globalData.bindInfo.sign;
    }
    var openid = app.globalData.openid;
    if (openid == "" || openid == null || openid==undefined){
      return;
    }
    var link = app.globalData.schlink;//学校域名
    var that = this;
    wx.request({
      url: getApp().globalData.baseUrl + 'classrank',
      method: "GET",
      data: {
        uid: uid,
        sign: sign,
        openid: openid,
        link: link
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 200) {
          that.setData({
            classdata: res.data.data
          });
        }
        that.setData({
          loadingHidden: true,
        });
      }
    })
  },
  //获取全校排名数据
  getschooldata: function () {
    var link = app.globalData.schlink;//学校域名
    var openid = app.globalData.openid;//
    var that = this;
    wx.request({
      url: getApp().globalData.baseUrl + 'schoollead',
      method: "GET",
      data: {
        link: link,
        openid:openid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.data) {
          if (res.data.data.length <1){
              that.setData({
                schooldata:false,
                loadingHidden: true
              });
          }else{
            that.setData({
              schooldata: res.data.data,
              loadingHidden: true
            });
          }
        }
      }
    })
  },
  //获取头像
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})